define(function(require) {
    "use strict";
    var ko = require("knockout");
    var router = require("plugins/router");
    var layui = require("layui");
    var $ = require("jquery");
    var system = require("durandal/system");
    var isFinishTableTemp = require("text!./isFinish-table-temp.html");
    var isRunningTableTemp = require("text!./isRunning-table-temp.html");
    var listTableBar = require("text!./list-table-bar.html");
    var viewBar = require("text!./view-bar.html");

    var acceptPageSize = [10, 30, 60, 90, 150, 300];
    var param = {
        nameLike: ko.observable(""),
        chineseNameLike: ko.observable(""),
        curr: 1,
        limit: acceptPageSize[0]
    };

    var tableModel = {
        id: "account-list",
        elem: "#list-table", //指定原始表格元素选择器（推荐id选择器）
        height: "full-227", //容器高度
        cols: [[
            //标题栏
            { field: "id", title: "ID", width: 50, fixed: "left" },
            { title: "分析", width: 400, align: "center", toolbar: viewBar },
            { field: "name", title: "计划名称", width: 300 },
            { field: "rootUrl", title: "引导链接", width: 300 },
            { field: "isFinish", title: "是否已完成", width: 100, templet: isFinishTableTemp },
            { field: "isRunning", title: "是否运行中", width: 100, templet: isRunningTableTemp },
            { field: "minuteLimit", title: "最大请求（分）", width: 100 },
            { field: "maxOccurs", title: "最大并发", width: 100 },
            { field: "maxDeep", title: "最大深度", width: 100 },
            { field: "lastestRunTime", title: "最近启动时间", width: 180 },
            { title: "操作", fixed: "right", width: 220, align: "center", toolbar: listTableBar }
        ]], //设置表头
        url: "/back/plans",
        page: {
            limits: acceptPageSize,
            limit: param.limit,
            curr: param.curr
        },
        done: function(res, curr, count) {
            //设置当前页码
            param.curr = curr;
            //设置当前分页大小
            param.limit = tableIns.config.limit;
        },
        response: {
            statusCode: 200
        }
    };

    var tableIns = null;

    return {
        //router: router,
        param: param,
        /*binding: function() {
            router.activeInstruction();
            return { cacheViews: true }
        },*/
        compositionComplete: function() {
            layui.use(["laytpl", "table", "element", "layer", "laydate", "form"], function() {
                var laytpl = layui.laytpl;
                var table = layui.table;
                var element = layui.element;
                var layer = layui.layer;
                var laydate = layui.laydate;
                var form = layui.form;

                tableModel.where = {
                    nameLike: param.nameLike(),
                    chineseNameLike: param.chineseNameLike()
                };
                tableModel.page.curr = param.curr;
                tableModel.page.limit = param.limit;

                tableIns = table.render(tableModel);

                table.on("tool(list-table)", function(obj) {
                    var data = obj.data;
                    if (obj.event === "update") {
                        router.navigate("plan/backAccountUpdate/" + data.id);
                    } else if (obj.event === "start") {
                        layer.confirm("确定开始本次抓取？", function(index) {
                            $.ajax({
                                type: "get",
                                //url: "/back/plan/start/" + data.id,
                                url: "/back/test02/" + data.id,
                                dataType: "json",
                                contentType: "application/json",
                                data: JSON.stringify(obj.data)
                            }).then(function(data) {
                                layer.alert(data.msg, function(index) {
                                    layer.close(index);
                                    tableIns.reload({ curr: param.curr });
                                });
                            }, function(data) {
                                layer.alert(data.responseJSON.msg);
                            });
                            layer.close(index);
                        });
                    } else if (obj.event === "delete") {
                        layer.confirm("确认删除？", function(index) {
                            $.ajax({
                                type: "DELETE",
                                url: "/back/plan/" + obj.data.id,
                                dataType: "json",
                                contentType: "application/json"
                            }).then(function(data) {
                                layer.alert(data.msg, function(index) {
                                    layer.close(index);
                                    tableIns.reload({ curr: param.curr });
                                });
                            }, function(data) {
                                layer.alert(data.responseJSON.msg);
                            });
                            layer.close(index);
                        });
                    } else if (obj.event === "review") {
                        router.navigate("plan/review/param/" + data.id);
                    } else if (obj.event === "keyword") {
                        router.navigate("plan/keyword/" + data.id);
                    } else if (obj.event === "sourceKeyword") {
                        router.navigate("plan/sourceKeyword/" + data.id);
                    } else if (obj.event === "trace") {
                        router.navigate("plan/trace/" + data.id);
                    } else if (obj.event === "each") {
                        router.navigate("plan/actions/" + data.id);
                    }

                });

                form.on("submit(search-submit)", function(data) {
                    tableIns.reload({
                        where: {
                            nameLike: param.nameLike(),
                            chineseNameLike: param.chineseNameLike()
                        },
                        page: {
                            curr: 1
                        }
                    });
                    return false;
                });

                form.on("submit(create-request)", function(data) {
                    router.navigate("plan/backAccountCreate");
                    return false;
                });
            });
        }
    };
});